package com.yun.zhong.fu.cloudgateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/**
 * @Author RenPu
 * @Date 2021/1/5 13:10
 * @Version 1.0
 * @Description: 实现自定义过过滤器
 **/

@Slf4j
@Component
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {


    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return ((exchange, chain) ->{
            log.info("请求进来了...{}...{}",config.getName(),config.getValue());
            ServerHttpRequest request = exchange.getRequest()
                                                .mutate()
                                                .build();
            ServerWebExchange mutate = exchange.mutate()
                                               .request(request)
                                               .build();
            return chain.filter(mutate);

        }) ;
    }
}
